<template>
  <el-container>
    <el-header>
      <div class="title-block">
        <div class="title-box">
          <span style="font-size: 22px; color: #fff"
            >文件递交系统 <i class="el-icon-s-promotion"></i
          ></span>
        </div>
      </div>
      <ul class="exit-block">
        <li class="exit-item" @click="logout">
          <i class="el-icon-switch-button"></i> 注销
        </li>
      </ul>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <ul class="aside-menu">
          <li class="menu-item menu-main">
            <a href=""><i class="el-icon-s-custom"></i> 人员库管理</a>
          </li>
          <li class="menu-item">
            <a href="" @click.prevent="linkTo(1)">收集人员库</a>
          </li>
          <li class="menu-item">
            <a href="" @click.prevent="linkTo(2)">提交人员库</a>
          </li>
          <li class="menu-item menu-main">
            <a href=""><i class="el-icon-tickets"></i> 任务管理</a>
          </li>
          <li class="menu-item">
            <a href="#" @click.prevent="linkTo(3)">任务列表</a>
          </li>
        </ul>
      </el-aside>
      <el-main>
        <el-row class="info-line">
          <div class="search-block" style="float: right; display: block">
            <div class="wrapper" style="margin-right: 80%">
              <div class="search-form">
                <div
                  class="autocomplete-container"
                  style="height: 25px; width: 278px"
                >
                  <input
                    type="text"
                    autocomplete="off"
                    name="query"
                    placeholder="搜索"
                    class="autocomplete-input"
                    style="
                      width: 200px;
                      height: 25px;
                      border-radius: 3px 0px 0px 3px;
                    "
                  />
                  <div
                    class="autocomplete-button"
                    style="height: 25px; line-height: 10px"
                  >
                    <i class="el-icon-search"></i>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </el-row>

        <el-row>
          <el-col :span="6">
            <div class="status-block">
              <div class="title-block">
                <h2>任务状态</h2>
              </div>
              <ul class="nav-block">
                <li
                  class="nav-item"
                  :class="[activeIndex === 1 ? 'active' : 'unactive']"
                >
                  <a @click.prevent="selectListChange(-1)">
                    <strong>全部</strong>
                  </a>
                </li>
                <li
                  class="nav-item"
                  :class="[activeIndex === 2 ? 'active' : 'unactive']"
                >
                  <a @click.prevent="selectListChange(0)">
                    <strong>未开始</strong>
                  </a>
                </li>
                <li
                  class="nav-item"
                  :class="[activeIndex === 3 ? 'active' : 'unactive']"
                >
                  <a @click.prevent="selectListChange(1)">
                    <strong>运行中</strong>
                  </a>
                </li>
                <li
                  class="nav-item"
                  :class="[activeIndex === 4 ? 'active' : 'unactive']"
                >
                  <a @click.prevent="selectListChange(2)">
                    <strong>已结束</strong>
                  </a>
                </li>
                <li
                  class="nav-item"
                  :class="[activeIndex === 5 ? 'active' : 'unactive']"
                >
                  <a @click.prevent="selectListChange(3)">
                    <strong>暂停中</strong>
                  </a>
                </li>
              </ul>
            </div>
          </el-col>
          <el-col :span="18">
            <div class="taskList">
              <div
                class="task"
                v-for="(item, index) in items"
                :key="item"
                :offset="index"
              >
                <div class="block-title">
                  <h1>{{ item.title }}</h1>
                  <span class="pull-right">
                    <h3>提交: {{ item.sstatus }}/{{ item.dstatus }}</h3>
                    <h3>状态: {{ item.status }}</h3>
                    <a href="">周期: {{ item.stime }} ~ {{ item.etime }}</a>
                  </span>
                </div>
                <el-button
                  class="btn btn-primary"
                  @click.native="linkToInfo(item.id)"
                  >详情</el-button
                >
                <el-dropdown class="btn-group">
                  <el-button
                    class="btn btn-default"
                    @click.native="linkToResult(item.id)"
                  >
                    结果<i class="el-icon-arrow-down el-icon--right"></i>
                  </el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>已完成情况</el-dropdown-item>
                    <el-dropdown-item>未完成情况</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <div class="pull-right">
                  <a
                    href=""
                    class="btn btn-success"
                    @click.prevent="startTask(item.id, item.status)"
                    ><i
                      :class="[
                        item.isStart === '运行'
                          ? 'el-icon-video-play'
                          : 'el-icon-video-pause',
                      ]"
                    ></i>
                    {{ item.isStart }}</a
                  >
                  <el-dialog
                    title="提示"
                    :visible.sync="dialogVisible"
                    width="30%"
                  >
                    <span> {{ message }}: </span>
                    <el-date-picker
                      type="date"
                      v-model="deadline"
                      placeholder="选择一个或多个日期"
                    >
                    </el-date-picker>
                    <span slot="footer" class="dialog-footer">
                      <el-button @click="dialogVisible = false"
                        >取 消</el-button
                      >
                      <el-button
                        type="primary"
                        @click="checkDate(item.id, 'start')"
                        >确 定</el-button
                      >
                    </span>
                  </el-dialog>
                  <a
                    href=""
                    class="btn btn-danger"
                    @click.prevent="deleteTask(index)"
                    ><i class="el-icon-delete"></i> 删除</a
                  >
                </div>
              </div>
            </div>

            <div class="block" style="float: right">
              <el-pagination
                @current-change="handleCurrentChange"
                :current-page="pageObject.pageNum"
                :page-size="pageObject.pageSize"
                layout="total, prev, pager, next, jumper"
                :total="pageObject.totalPage"
              >
              </el-pagination>
            </div>
          </el-col>
        </el-row>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getTaskList } from "@/api/admin.js";
import { formatDate } from "@/utils/dateformat.js";
import { start, pause } from "@/api/admin.js";
import { delTask } from "@/api/brand.js";

const pageParam = {
  list: [],
  pageNum: null,
  pageSize: null,
  total: null,
  totalPage: null,
};
export default {
  data() {
    return {
      items: [],
      multipleSelection: [],
      currentPage: 1,
      pageObject: null,
      activeIndex: 1,
      status: -1,
      dialogVisible: false,
      message: "",
      warn: "",
      deadline: "",
    };
  },

  created() {
    this.pageObject = Object.assign({}, pageParam);
    this.getPageData("", "", -1);
    console.log(this.items);
  },

  methods: {
    logout() {
      this.$store.dispatch("LogOut").then((response) => {});
      this.$router.push({
        path: "/login",
      });
    },
    checkDate(tid, opt) {
      this.warn = "";
      if (this.deadline === "") this.warn = "请选择日期！";
      else {
        var date = formatDate(new Date());
        this.deadline = formatDate(this.deadline);
        console.log(this.deadline);
        console.log(date);
        if (this.deadline <= date) this.warn = "选择的日期不能小于现有日期";
        else {
          if (opt === "update")
            changeDate(tid, this.deadline).then((response) => {});
          else start(tid, this.deadline).then((response) => {});
        }
      }
      if (this.warn != "") this.$message(this.warn);
    },
    deleteTask(index) {
      this.$confirm(
        "是否永久删除任务: " + this.items[index].title + "?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        delTask(this.task.id).then((response) => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
    startTask(tid, status) {
      if (status === "已结束") {
        this.message = "该任务已结束，选择新的截至日期以开始任务   ";
        this.dialogVisible = true;
      } else if (status === "运行中") {
        pause(tid).then((response) => {
          this.getPageData("", "", -1);
        });
      } else {
        start(tid, "").then((response) => {
          this.getPageData("", "", -1);
        });
      }
    },
    linkTo(index) {
      if (index === 3) {
        this.$router.push({
          path: "/adminTask",
        });
      } else if (index === 2) {
        this.$router.push({
          path: "/adminSubData",
        });
      } else {
        this.$router.push({
          path: "/admin",
        });
      }
    },
    linkToInfo(id) {
      this.$router.push({
        path: "/adminTaskInfo",
        query: { tid: id },
      });
    },
    linkToResult(id) {
      this.$router.push({
        path: "/adminTaskSlist",
        query: { tid: id },
      });
    },
    selectListChange(status) {
      this.status = status;
      this.activeIndex = status + 2;
      this.getPageData(this.pageObject.pageSize, this.currentPage, status);
    },
    linkTo(index) {
      if (index === 3) {
        this.$router.push({
          path: "/adminTask",
        });
      } else if (index === 2) {
        this.$router.push({
          path: "/adminSubData",
        });
      } else {
        this.$router.push({
          path: "/admin",
        });
      }
    },
    getTaskStatus(status) {
      var name = "";
      switch (status) {
        case 0:
          name = "未开始";
          break;
        case 1:
          name = "运行中";
          break;
        case 2:
          name = "已结束";
          break;
        case 3:
          name = "暂停中";
          break;
      }
      return name;
    },
    getPageData(pageSize, currentPage, status) {
      this.items.splice(0, this.items.length);
      getTaskList(pageSize, currentPage, status).then((response) => {
        let data = response.data;
        this.pageObject.list = data.list;
        this.pageObject.pageNum = data.pageNum;
        this.pageObject.pageSize = data.pageSize;
        this.pageObject.total = data.total;
        this.pageObject.totalPage = data.totalPage;
        for (var i = 0; i < this.pageObject.list.length; i++) {
          let item = {};
          item.id = this.pageObject.list[i].id;
          item.title = this.pageObject.list[i].title;
          item.status = this.getTaskStatus(this.pageObject.list[i].status);
          item.stime = formatDate(this.pageObject.list[i].createTime);
          item.etime = formatDate(this.pageObject.list[i].deadline);
          item.sstatus = this.pageObject.list[i].sstatus;
          if (item.sstatus === null) item.sstatus = 0;
          item.dstatus = this.pageObject.list[i].dstatus;
          item.isStart = item.status === "运行中" ? "暂停" : "运行";
          this.items.push(item);
        }
      });
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.getPageData(this.pageObject.pageSize, this.currentPage, this.status);
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style scoped>
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
body {
  margin: 0;
  padding: 0;
  line-height: 24px;
  font: 14px Helvetica Neue, Helvetica, PingFang SC, 微软雅黑, Tahoma, Arial,
    sans-serif;
}

.el-header {
  width: 100%;
  background: #009688;
}

.el-aside {
  top: 60px;
  background-color: #393d49;
  overflow: hidden;
  height: 100%;
  position: fixed;
  left: 0;
  z-index: 999;
}

.el-main {
  left: 200px;
  background-color: #eeebf2;
  padding: 20px;
  position: absolute;
  width: 88%;
  bottom: 0;
  top: 60px;
}

.exit-block {
  position: absolute;
  right: 10px;
  top: 0;
}

.exit-item {
  list-style: none;
  color: white;
  margin: 0 15px;
  line-height: 30px;
}

.title-box {
  left: 0;
  width: 300px;
  line-height: 60px;
}

.el-header .title-block {
  align-items: center;
  width: 185px;
  height: 70px;
}

.autocomplete-input {
  padding: 9px;
  border-radius: 3px;
  font-family: inherit;
  float: left;
  font-size: 1em;
  border: 1px solid rgba(0, 0, 0, 0.19);
  margin: 0;
}

.autocomplete-button {
  font-family: inherit;
  border: none;
  background-color: #990101;
  color: white;
  padding: 8px;
  float: left;
  cursor: pointer;
  border-radius: 0px 3px 3px 0px;
  transition: all 0.2s ease-out 0s;
  margin: 0.5px 0px 0px -1px;
}
.autocomplete-button {
  background-color: #009688 !important;
}

.aside-menu {
  margin: 0;
  padding: 0;
}

.aside-menu li {
  list-style: none;
}

.aside-menu .menu-item {
  display: block;
  width: 100%;
  line-height: 45px;
}

.aside-menu .menu-item a {
  color: rgba(255, 255, 255, 0.7);
}

.aside-menu .menu-main a {
  color: white;
}
.aside-menu .menu-main {
  background-color: #23262e;
}

.aside-menu .menu-item a {
  position: relative;
  height: 45px;
  line-height: 45px;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  display: block;
  padding: 0 20px;
  transition: all 0.3s;
  -webkit-transition: all 0.3s;
}
a {
  text-decoration: none;
}

.el-col-6 {
  color: #353a4d;
  padding-left: 15px;
  padding-right: 15px;
}
.status-block {
  padding: 20px;
  padding-bottom: 10px;
  margin: 0 0 10px;
  margin-bottom: 10px;
  background-color: #fff;
  border-top-left-radius: 2px;
  border-top-right-radius: 2px;

  box-shadow: 0 2px 0 rgba(223, 218, 232, 0.5);
}

.el-main .title-block {
  background: rgba(223, 218, 232, 0.15);
  border-bottom-color: #dfdae8;
  margin: -20px -15px 20px;
  margin-left: -20px;
  margin-right: -20px;

  border-bottom: 2px solid #dae0e8;
  border-top-left-radius: 2px;
  border-top-right-radius: 2px;
}

.el-main .title-block h2 {
  display: inline-block;
  font-size: 14px;
  line-height: 1.4;
  margin: 0;
  padding: 10px 15px 9px;
  font-weight: 600;
  text-transform: uppercase;
}

.el-main .title-block .nav-block {
  margin-bottom: 0;
  padding-left: 0;
  list-style: none;
  margin-top: 0;
}

.nav-block {
  margin: 0;
  padding: 0;
}

.nav-block > li {
  float: none;
  display: block;
  position: relative;
}

.nav-block > li > a {
  position: relative;
  display: block;
  padding: 10px 15px;
  margin: 4px 0 0;
}

strong {
  font-weight: 600;
}

.active {
  color: #fff;
  background-color: #009688;
}

.info-line {
  background: rgba(255, 255, 255, 0.75);
  margin: -20px -20px 20px;
  padding: 10px 20px;
}

.el-col-18 {
  padding-left: 15px;
  padding-right: 15px;
  min-height: 1px;
}

.taskList {
  padding: 20px 15px;
  padding-left: 20px;
  padding-right: 20px;
  box-shadow: 0 2px 0 rgba(223, 218, 232, 0.5);
  margin-bottom: 20px;
  margin: 0 0 10px;
  background-color: #ffffff;
  border-top-left-radius: 2px;
  border-top-right-radius: 2px;
}

.task {
  padding: 20px 15px;
  padding-left: 20px;
  padding-right: 20px;
  border: 1px solid #dae0e8;
  -webkit-box-shadow: none;
  box-shadow: none;
  background-color: #ffffff;
  border-top-left-radius: 2px;
  border-top-right-radius: 2px;
  margin: 0 0 10px;
}

.block-title {
  margin: -20px -15px 20px;
  border-bottom: 2px solid #dae0e8;
  border-top-left-radius: 2px;
  border-top-right-radius: 2px;
  background: rgba(223, 218, 232, 0.15);
  margin-left: -20px;
  margin-right: -20px;
}

.block-title h1,
h2,
h3 {
  display: inline-block;
  font-size: 14px;
  line-height: 1.4;
  margin: 0;
  padding: 10px 15px 9px;
  font-weight: 600;
  text-transform: uppercase;
}

.block-title a {
  font-size: 14px;
  line-height: 1.42857143;
  color: #7c62ad;
  background-color: transparent;
}

.pull-right {
  float: right !important;
}

.btn {
  margin: 1px 0;
  background-color: #ffffff;
  border-radius: 3px;
}

.btn-primary {
  background-color: #7c62ad;
  border-color: #7c62ad;
  color: white;
  width: 80px;
}

.btn-success {
  background-color: #afde5c;
  border-color: #afde5c;
  width: 80px;
  color: white;
}

.btn-danger {
  background-color: #de815c;
  border-color: #de815c;
  width: 80px;
  color: white;
}

.btn-default {
  background-color: #eeeeee;
  border-color: #eeeeee;
  width: 80px;
}
.btn {
  display: inline-block;
  margin-bottom: 0;
  font-weight: normal;
  text-align: center;
  vertical-align: middle;
  -ms-touch-action: manipulation;
  touch-action: manipulation;
  cursor: pointer;
  background-image: none;
  border: 1px solid transparent;
  white-space: nowrap;
  padding: 6px 12px;
  font-size: 14px;
  line-height: 1.42857143;
  border-radius: 4px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
</style>